package com.yyhh.helpcall.activity;

import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.material.button.MaterialButton;
import com.yyhh.helpcall.BuildConfig;
import com.yyhh.helpcall.R;
import com.yyhh.helpcall.http.OkHttpUtils;
import com.yyhh.helpcall.http.bean.BaseResponse;
import com.yyhh.helpcall.http.bean.LoginResponse;
import com.yyhh.helpcall.utils.AppUtils;
import com.yyhh.helpcall.utils.NetLog;
import com.yyhh.helpcall.utils.SpUtils;
import com.yyhh.helpcall.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;
import java.util.HashMap;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {

    private EditText phone,pass;
    private MaterialButton login,cancle;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);
        initViews();
    }

    private void initViews(){
        phone = findViewById(R.id.phone);
        pass = findViewById(R.id.pass);
        login = findViewById(R.id.login);
        cancle = findViewById(R.id.cancle);
        title = findViewById(R.id.title);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if(!isTaskRoot()){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            title.setTranslationX(-100);
        }else{
            title.setTranslationX(-20);
        }

//        if(BuildConfig.DEBUG){
            phone.setText("13366810662");
            pass.setText("123456");
//        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startLogin();
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void startLogin(){
        String account = phone.getText().toString().trim();
        String password = pass.getText().toString().trim();

        if(!AppUtils.isMobileNo(account)){
            ToastUtils.show("请输入正确的手机号码");
            return;
        }
        if(TextUtils.isEmpty(password)){
            ToastUtils.show("请输入密码");
            return;
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("account",account);
        map.put("password",password);
        MobclickAgent.onEventObject(this,"login",map);

        showProgressDialog();
        OkHttpUtils.getInstance().getApi().login(account,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<LoginResponse>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<LoginResponse> value) {
                        hiddenProgressDialog();
                        if(value.isSuccess()){
                            //存储token
                            SpUtils.getInstance(LoginActivity.this).putString("account",account);
                            SpUtils.getInstance(LoginActivity.this).putString("token",value.data.token);

                            //设置别名
                            JPushInterface.setAlias(getApplicationContext(),"tel_"+account,new TagAliasCallback(){

                                @Override
                                public void gotResult(int i, String s, Set<String> set) {
                                    NetLog.d("setAlias " + s + " result = " + i);
                                }
                            });
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("isNewLogin",true);
                            startActivity(intent);
                            finish();
                        }else{
                            value.showErrorMessage();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        ToastUtils.show("出现错误,请重试~"+e.toString());
                        hiddenProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        hiddenProgressDialog();
                    }
                });
    }
}
